﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NXBSaiGon
{
    public partial class CheckOut : System.Web.UI.Page
    {
        Basket basket = new Basket();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btProvinceRefresh_Click(object sender, EventArgs e)
        {
            ddlToProvince.Items.Clear();
            string TranSer = ddlTranService.SelectedValue;
            switch (TranSer)
            {
                case "VC15":
                    VC15.Webservice vc15 = new VC15.Webservice();
                    string vc15Key = vc15.WSLogin("KD12-65", "KD12-65");
                    VC15.Province[] vc15provinces = vc15.GetProvinceName();
                    foreach (VC15.Province pro in vc15provinces)
                    {
                        ddlToProvince.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
                case "VC07":
                    VC07.Service vc07 = new VC07.Service();
                    string vc07Key = vc07.WSLogin("KD12", "KD12");
                    VC07.Province[] vc07provinces = vc07.GetProvinceName();
                    foreach (VC07.Province pro in vc07provinces)
                    {
                        ddlToProvince.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
                case "VC11":
                    VC11.Service1 vc11 = new VC11.Service1();
                    string vc11Key = vc11.WSLogin("sv", "sv");
                    VC11.Province[] vc11provinces = vc11.GetProvinceName();
                    foreach (VC11.Province pro in vc11provinces)
                    {
                        ddlToProvince.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
                case "VC13":
                    VC13.Service vc13 = new VC13.Service();
                    string vc13Key = vc13.WSLogin("123", "123");
                    VC13.Province[] vc13provinces = vc13.GetProvinceName();
                    foreach (VC13.Province pro in vc13provinces)
                    {
                        ddlToProvince.Items.Add(new ListItem(pro.ProvinceName, pro.ID));
                    }
                    break;
            }
        }

        protected void Button_XuLy_Click(object sender, EventArgs e)
        {
            string bank = ddlbank.SelectedValue;
            string TranSer = ddlTranService.SelectedValue;

            string ToProvince = ddlToProvince.SelectedValue;
            string ToAddress = txtToAddress.Text;
            string FromAddress = "Nhà Xuất Bản 12";
            string CreditNumber = txtCreditNumber.Text;
            string FromProvinceID = "";
            string buyItCC = "";

            Basket gio = (Basket)Session["GioHang"];
            if (gio == null)
                Session["GioHang"] = new Basket();
            int total = gio.Total;
            int quantity = gio.Quantity;
            if (quantity == 0)
            {
                lblCheckOutResult.Text = "<span style='color:red;'>Giỏ hàng của bạn trống</span>";
                return;
            }

            string bKey = "";
            string tKey = "";

            string tranKey = "";
            double shippingPrice = 100;
            int money = 0;
            DateTime strNow = DateTime.Now;
            int bTranResult = 0;
            string transportString = gio.getTransportString();

            bool havingErrors = false;

            //Calculating ShippingPrice
            switch (TranSer)
            {
                case "VC15":
                    FromProvinceID = "HCM";
                    //Declare ws object
                    VC15.Webservice vc15 = new VC15.Webservice();

                    //Get ShippingPrice
                    VC15.TransportType[] vc15TranType = vc15.GetTransportType();
                    shippingPrice = vc15.ShippingPrice(FromProvinceID, ToProvince, vc15TranType[0].ID, transportString);
                    if (shippingPrice <= 0)
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC07":
                    FromProvinceID = "5";
                    //Login
                    VC07.Service vc07 = new VC07.Service();

                    //Get ShippingPrice
                    VC07.TransportType[] vc07TranType = vc07.GetTransportType();
                    shippingPrice = vc07.ShippingPrice(FromProvinceID, ToProvince, vc07TranType[0].ID, transportString);
                    if (shippingPrice <= 0)
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC11":
                    FromProvinceID = "HCM";
                    //Login
                    VC11.Service1 vc11 = new VC11.Service1();

                    //Get ShippingPrice
                    VC11.TransportType[] vc11TranType = vc11.GetTransportType();
                    shippingPrice = vc11.ShippingPrice(FromProvinceID, ToProvince, vc11TranType[0].ID, transportString);// + quantity.ToString());
                    //shippingPrice = vc11.ShippingPrice("AG", "AG", "LVC01", "a: 2");
                    if (shippingPrice <= 0)
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC13":
                    FromProvinceID = "5";
                    //Login
                    VC13.Service vc13 = new VC13.Service();

                    //Get ShippingPrice
                    VC13.TransportType[] vc13TranType = vc13.GetTransportType();
                    shippingPrice = vc13.ShippingPrice(FromProvinceID, ToProvince, vc13TranType[0].ID, transportString);
                    if (shippingPrice <= 0)
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                default:
                    havingErrors = true;
                    break;
            }
            if (havingErrors == true)
            {
                lblCheckOutResult.Text = "Có lỗi trong quá trình tính giá vận chuyển. Hãy thử lại lần nữa!";
                return;
            }
            money = total + (int)shippingPrice;



            //Using bank service to transfer money
            switch (bank)
            {
                case "NH08":
                    NH08.WSMoGioi nh08 = new NH08.WSMoGioi();
                    //Login
                    bKey = nh08.Login("nxb12", "123456");
                    if (bKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Transfer money
                    buyItCC = "5115308248601534";

                    bTranResult = nh08.TransferMoney(bKey, CreditNumber, buyItCC, money);
                    if (bTranResult <= 0)
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "NH04":
                    NH04.Service1 nh04 = new NH04.Service1();
                    //Login
                    bKey = nh04.Login("nxb12", "nxb12");
                    if (bKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Transfer money
                    buyItCC = "5114808267693537";

                    bTranResult = nh04.TransferMoney(bKey, CreditNumber, buyItCC, money);
                    if (bTranResult <= 0)
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "NH01":
                    NH01.Service1 nh01 = new NH01.Service1();
                    //Login
                    bKey = nh01.Login("nxb12@yahoo.com", "12345678");
                    if (bKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    buyItCC = "4024007181015974";

                    bTranResult = nh01.TransferMoney(bKey, CreditNumber, buyItCC, money);
                    if (bTranResult <= 0)
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                default:
                    havingErrors = true;
                    break;
            }
            if (havingErrors == true)
            {
                lblCheckOutResult.Text = "Có lỗi khi chuyển tiền. Vui lòng kiểm tra lại thẻ tín dụng, sử dụng dịch vụ ngân hàng khác hoặc thử lại sau!";
                return;
            }
            lblCheckOutResult.Text = bTranResult.ToString();

            //Place transport command
            switch (TranSer)
            {
                case "VC15":
                    FromProvinceID = "HCM";
                    //Login
                    VC15.Webservice vc15 = new VC15.Webservice();
                    tKey = vc15.WSLogin("KD12-65", "KD12-65");
                    if (tKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    VC15.TransportType[] vc15TranType = vc15.GetTransportType();
                    tranKey = vc15.PlaceTransportCommand(tKey, FromProvinceID, ToProvince, FromAddress, ToAddress, vc15TranType[0].ID, transportString, strNow.AddDays(2));
                    if (tranKey == "0" || tranKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC07":
                    FromProvinceID = "5";
                    //Login
                    VC07.Service vc07 = new VC07.Service();
                    tKey = vc07.WSLogin("kd12", "kd12");
                    if (tKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    VC07.TransportType[] vc07TranType = vc07.GetTransportType();
                    tranKey = vc07.PlaceTransportCommand(tKey, FromProvinceID, ToProvince, FromAddress, ToAddress, vc07TranType[0].ID, transportString, strNow.AddDays(2));
                    //transkey = vc07.PlaceTransportCommand(key, "1", "2", "123", "321", "1", "a : 1", strNow.AddDays(2));
                    if (tranKey == "0" || tranKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC11":
                    FromProvinceID = "HCM";
                    //Login
                    VC11.Service1 vc11 = new VC11.Service1();
                    tKey = vc11.WSLogin("sv", "sv");
                    if (tKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    VC11.TransportType[] vc11TranType = vc11.GetTransportType();
                    tranKey = vc11.PlaceTransportCommand(tKey, FromProvinceID, ToProvince, FromAddress, ToAddress, vc11TranType[0].ID, transportString, strNow.AddDays(2));
                    if (tranKey == "0" || tranKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
                case "VC13":
                    FromProvinceID = "5";
                    //Login
                    VC13.Service vc13 = new VC13.Service();
                    tKey = vc13.WSLogin("123", "123");
                    if (tKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }

                    //Place command
                    VC13.TransportType[] vc13TranType = vc13.GetTransportType();
                    tranKey = vc13.PlaceTransportCommand(tKey, FromProvinceID, ToProvince, FromAddress, ToAddress, vc13TranType[0].ID, transportString, strNow.AddDays(2));
                    if (tranKey == "0" || tranKey == "-1")
                    {
                        havingErrors = true;
                        break;
                    }
                    break;
            }
            lblCheckOutResult.Text = "Thanh Toán đã xong";
            lblTotalMoney.Text = money.ToString();
            lblWSResult.Text = bTranResult.ToString();
            lblTranKey.Text = "<a href='" + tranKey.ToString() + "' >" + tranKey.ToString() + "</a>";
            lblTranSer.Text = TranSer;
        }

        protected void btTranTypeRefresh_Click(object sender, EventArgs e)
        {
            ddlTransportType.Items.Clear();
            string TranSer = ddlTranService.SelectedValue;
            switch (TranSer)
            {
                case "VC15":
                    VC15.Webservice vc15 = new VC15.Webservice();
                    string vc15Key = vc15.WSLogin("KD12-65", "KD12-65");
                    VC15.TransportType[] vc15trantype = vc15.GetTransportType();
                    foreach (VC15.TransportType t in vc15trantype)
                    {
                        ddlTransportType.Items.Add(new ListItem(t.TransportTypeName, t.ID));
                    }
                    break;
                case "VC07":
                    VC07.Service vc07 = new VC07.Service();
                    string vc07Key = vc07.WSLogin("KD12", "KD12");
                    VC07.TransportType[] vc07trantype = vc07.GetTransportType();
                    foreach (VC07.TransportType t in vc07trantype)
                    {
                        ddlTransportType.Items.Add(new ListItem(t.TransportTypeName, t.ID));
                    }
                    break;
                case "VC11":
                    VC11.Service1 vc11 = new VC11.Service1();
                    string vc11Key = vc11.WSLogin("sv", "sv");
                    VC11.TransportType[] vc11trantype = vc11.GetTransportType();
                    foreach (VC11.TransportType t in vc11trantype)
                    {
                        ddlTransportType.Items.Add(new ListItem(t.TransportTypeName, t.ID));
                    }
                    break;
                case "VC13":
                    VC13.Service vc13 = new VC13.Service();
                    string vc13Key = vc13.WSLogin("123", "123");
                    VC13.TransportType[] vc13trantype = vc13.GetTransportType();
                    foreach (VC13.TransportType t in vc13trantype)
                    {
                        ddlTransportType.Items.Add(new ListItem(t.TransportTypeName, t.ID));
                    }
                    break;
            }
        }
    }
}